﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ListSamples
{
    class Book : IComparable<Book>, IFormattable//继承两个接口
                                                 //IComparable<T> 接口定义由值类型或类实现的通用比较方法，以为排序实例创建类型特定的比较方法。
                                                 //IFormattable 接口 提供一种功能，用以将对象的值格式化为字符串表示形式。
    {

        public string BookId { get; }
        public string BookName { get; }
        public double  Price { get; }
        public string Houston { get; }


        public Book(string bookId, string bookName, double  price, string houston)
        {
            BookId = bookId;
            BookName = bookName;
            Price = price;
            Houston = houston;

        }
        public override string ToString() =>$"出版号：{BookId} 书名：{BookName} 价格：{Price} 出版社：{Houston}";

        public virtual string ToString(string format) => ToString(format, null);


        public string ToString(string format, IFormatProvider formatProvider)
        {
            switch (format)
            {
                case null:
                case "A":
                    return ToString();
                case "Id":
                    return $"出版号：{BookId}";
                case "Name":
                    return $"书名：{BookName}";
                case "NP":
                    return $"书名：{BookName} 价格：{Price}";
                case "H":
                    return $"出版社：{Houston}";
                case "C": return $"出版号：{BookId} 书名：{BookName} 价格：{Price} 出版社：{Houston}";
                default:
                    throw new FormatException($"invalid format string {format}");
            }
        }

        public int CompareTo(Book other)
        {
            int compare = BookName?.CompareTo(other?.BookName) ?? -1;  
            if (compare == 0)
            {
                return Houston?.CompareTo(other?.Houston) ?? -1;     
            }
            return compare;
        }
    }
}

